<!DOCTYPE html>


<html lang="zh-CN">
  

    <head>
      <meta charset="utf-8" />
       
      <meta name="keywords" content="ES6、JS" />
       
      <meta name="description" content="ES6、JS" />
      
      <meta
        name="viewport"
        content="width=device-width, initial-scale=1, maximum-scale=1"
      />
      <title>再识 ES6 |  君书</title>
  <meta name="generator" content="hexo-theme-ayer">
      
      <link rel="shortcut icon" href="/favicon.ico" />
       
<link rel="stylesheet" href="/dist/main.css">

      <link
        rel="stylesheet"
        href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css"
      />
      
<link rel="stylesheet" href="/css/custom.css">
 
      <script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>
       
 

      <link
        rel="stylesheet"
        href="https://cdn.jsdelivr.net/npm/@sweetalert2/theme-bulma@5.0.1/bulma.min.css"
      />
      <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.0.19/dist/sweetalert2.min.js"></script>

      <!-- mermaid -->
      
      <style>
        .swal2-styled.swal2-confirm {
          font-size: 1.6rem;
        }
      </style>
    <link rel="alternate" href="/atom.xml" title="君书" type="application/atom+xml">
</head>
  </html>
</html>


<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-再识-ES6"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  再识 ES6
</h1>
 

      
    </header>
     
    <div class="article-meta">
      <a href="/2021/04/27/%E5%86%8D%E8%AF%86-ES6/" class="article-date">
  <time datetime="2021-04-27T01:35:17.000Z" itemprop="datePublished">2021-04-27</time>
</a>   
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">1.8k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">7 分钟</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <p>ES2015 版本的 ECMAScript 语法可以称为 ES6。 ES6 可以泛指 ES2015 以及之后每年出的 ES 版本中的新语法。今天整理下 ES6 中主要的新增内容。</p>
<p>ES6 新增内容可以分为三大类：</p>
<ul>
<li><ol>
<li>解决原有语法的一些不足和问题</li>
</ol>
</li>
<li><ol start="2">
<li>对原有语法进行加强</li>
</ol>
</li>
<li><ol start="3">
<li>新增数据类型和数据结构</li>
</ol>
</li>
</ul>
<span id="more"></span>

<h2 id="一-解决原有语法的不足和问题"><a href="#一-解决原有语法的不足和问题" class="headerlink" title="一. 解决原有语法的不足和问题"></a>一. 解决原有语法的不足和问题</h2><p>ES6 之前 js 中的作用域分为两类：<code>全局变量和函数内部变量</code>。使用 <code>var</code> 声明的变量不具有块级作用域，所以在 <code>&#123;&#125;</code> 外仍可以访问。为了解决该问题，ES6 新增了 <code>let</code> 和 <code>const</code> 关键字声明变量。使用新增关键字声明的变量具有块级作用域，更有利于程序中的内存回收，所以在开发中更推荐使用 <code>let const</code> 声明变量。</p>
<h2 id="二-扩展原有语法"><a href="#二-扩展原有语法" class="headerlink" title="二. 扩展原有语法"></a>二. 扩展原有语法</h2><p>ES6 增加了一些操作对象的语法。下面整理下经常会使用到的新增特性：</p>
<ol>
<li><p>结构赋值</p>
<p>在 <code>=</code> 的左边使用 <code>&#123;&#125;</code> 或者 <code>[]</code> 定义按实际需要需获取的 <code>=</code> 右边对象或者数组内的属性。举例：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> &#123; name &#125; = &#123; <span class="attr">name</span>: <span class="string">&#x27;xiaofujun&#x27;</span>, <span class="attr">age</span>: <span class="number">18</span> &#125;;</span><br><span class="line"><span class="keyword">const</span> [ name， location ] = [ <span class="string">&#x27;xiaofujun&#x27;</span>, <span class="string">&#x27;SH&#x27;</span> ]；</span><br></pre></td></tr></table></figure></li>
<li><p><code>...</code> 运算</p>
<p> <code>...</code> 运算又称为<code>剩余运算符</code>，一般结合解构赋值一起使用。具体使用如下：</p>
 <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> &#123; name, ...rest &#125; = &#123; <span class="attr">name</span>: <span class="string">&#x27;xiaofujun&#x27;</span>, <span class="attr">age</span>: <span class="number">18</span> &#125;;</span><br><span class="line"><span class="keyword">const</span> [ name, ...rest ] = [ <span class="string">&#x27;xiaofujun&#x27;</span>, <span class="string">&#x27;SH&#x27;</span>, <span class="string">&#x27;1&#x27;</span> ];</span><br></pre></td></tr></table></figure>
<p> 获取到的 <code>rest</code> 是个对象或者数组。在使用 <code>...</code> 运算时需要注意，一定要将其放到最后一个元素，否则语法会报错。</p>
</li>
<li><p>标签字符串模板</p>
<p> 字符串模块我们经常使用，但是标签字符串模块确实我自己用的不多，可以说这个概念也是初次接触到。</p>
<ol>
<li><p>标签字符串模块的格式</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getInfo</span>(<span class="params">str, name, class1, age</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> str[<span class="number">0</span>] + name + str[<span class="number">1</span>] + class1 + str[<span class="number">2</span>] + (age &lt; <span class="number">18</span> ? age : <span class="string">&#x27;保密&#x27;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> [ name, age, class1 ] = [<span class="string">&#x27;xiaofujun&#x27;</span>, <span class="number">20</span>, <span class="string">&#x27;二班&#x27;</span>];</span><br><span class="line"><span class="comment">// 字符串模块 </span></span><br><span class="line"><span class="keyword">const</span> info = getInfo<span class="string">`studentInfo: name is <span class="subst">$&#123;name&#125;</span>, class is <span class="subst">$&#123;class1&#125;</span>, age is <span class="subst">$&#123;age&#125;</span>`</span>;</span><br><span class="line"><span class="built_in">console</span>.log(info)</span><br></pre></td></tr></table></figure></li>
<li><p>标签字符串模块的特点</p>
<ol>
<li>方法中接收的参数<ol>
<li>第一个参数是方法后跟的字符串以 <code>$&#123;&#125;</code> 分割后形成的数组。对应到示例中即为 <code>[&#39;studentInfo: name is &#39;, &#39;, class is &#39;, &#39;, age is &#39;]</code>；</li>
<li>其后的参数即为 <code>$&#123;&#125;</code> 中的变量</li>
</ol>
</li>
<li>方法的返回值可赋值为 <code>=</code> 左边的变量</li>
</ol>
</li>
<li><p>优点</p>
<p> 可在某个具体的方法中进行变量的转换赋值，将在字符串模块中的运算放置到一个方法中完成。</p>
</li>
</ol>
</li>
<li><p>对象属性的扩展</p>
<p> es6 之前获取对象的属性可以通过 <code>.</code> 运算，例如 <code>obj.name</code> ； es6 增加了 <code>[]</code> 获取属性的值，此时 <code>[]</code> 中可以存放变量，例如：</p>
 <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> key = <span class="string">&#x27;name&#x27;</span>;</span><br><span class="line"><span class="keyword">const</span> num = <span class="number">1</span>;</span><br><span class="line"><span class="keyword">const</span> obj = &#123; <span class="attr">name</span>: <span class="string">&#x27;xiao&#x27;</span>, <span class="attr">age</span>: <span class="number">24</span> &#125;;</span><br><span class="line">obj[num] = <span class="string">&#x27;数字&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(obj.name); <span class="comment">// &#x27;xiao&#x27;</span></span><br><span class="line"><span class="built_in">console</span>.log(obj[key]); <span class="comment">// &#x27;xiao&#x27;</span></span><br><span class="line"><span class="built_in">console</span>.log(obj[num]); <span class="comment">// ’数字‘</span></span><br></pre></td></tr></table></figure>
<p> 上述示例中虽然 <code>num</code> 的值是 <code>number</code> 类型，但是当其作为对象的属性使用时，在对象内部会将其转换为字符串。即在 es6 之前，普通对象的 <code>key</code> 只能为字符串，如果不是字符串，则会使用 <code>toString</code> 方法将其转换为字符串。为了解决该问题，ES6 引入了新的数据类型 <code>Map</code>。</p>
</li>
</ol>
<h2 id="三-新增的数据类型和方法"><a href="#三-新增的数据类型和方法" class="headerlink" title="三. 新增的数据类型和方法"></a>三. 新增的数据类型和方法</h2><ol>
<li><p>新增的数据类型</p>
<ol>
<li><p><code>Set</code></p>
<p>  Set 是 ES6 新增的用于存储数组的数据类型，Set 对象中存储的元素不存在重复元素。若向 Set 创建的对象中添加重复数据，则会跳过忽略执行。</p>
<ol>
<li><p>创建 Set 对象</p>
 <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> set = <span class="keyword">new</span> <span class="built_in">Set</span>();</span><br></pre></td></tr></table></figure></li>
<li><p>新增元素</p>
 <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">set.set(<span class="number">0</span>);</span><br><span class="line">set.set(<span class="number">2</span>);</span><br><span class="line">set.set(<span class="number">3</span>);</span><br></pre></td></tr></table></figure></li>
</ol>
</li>
<li><p><code>Map</code></p>
<p>  Map 是 ES6 新增的用于存储 <code>key:value</code> 的数据类型。传统的对象 <code>key</code> 是字符串类型的值，在 Map 中 key 可以是是任意类型。</p>
<ol>
<li><p>创建 Map 对象</p>
 <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> map = <span class="keyword">new</span> <span class="built_in">Map</span>();</span><br></pre></td></tr></table></figure></li>
<li><p>添加属性</p>
 <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">map.set(<span class="string">&#x27;name&#x27;</span>, <span class="string">&#x27;xiao&#x27;</span>);</span><br><span class="line">map.set(<span class="literal">true</span>, <span class="string">&#x27;1&#x27;</span>);</span><br></pre></td></tr></table></figure></li>
</ol>
</li>
<li><p><code>Symbol</code></p>
<p>  <code>Symbol()</code> 可以创建独一无二的值。</p>
</li>
</ol>
</li>
</ol>
<ol start="2">
<li><p>新增的对象</p>
<ol>
<li><p><code>Proxy</code></p>
<p>代理。可以对一个对象设置代理，通过访问另一个对象从而有选择的告诉用户想知道的信息。具体使用如下：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> star = &#123; <span class="attr">name</span>: <span class="string">&#x27;Tom&#x27;</span>, <span class="attr">age</span>: <span class="number">23</span>, <span class="attr">tel</span>: <span class="string">&#x27;xxx&#x27;</span> &#125;;</span><br><span class="line"><span class="keyword">const</span> agent = <span class="keyword">new</span> <span class="built_in">Proxy</span>(star, &#123;</span><br><span class="line">  get: <span class="function"><span class="keyword">function</span>(<span class="params">target, key</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(<span class="string">&#x27;tel&#x27;</span> === key) &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="string">&#x27;yyy&#x27;</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> target[key];</span><br><span class="line">  &#125;,</span><br><span class="line">  set: <span class="function"><span class="keyword">function</span>(<span class="params">target, key, value</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(<span class="string">&#x27;money&#x27;</span> === key) &#123;</span><br><span class="line">      <span class="keyword">if</span>(value &lt; <span class="number">100000</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">      &#125;</span><br><span class="line">      target[key] = value;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(agent.tel);  <span class="comment">// yyy</span></span><br><span class="line"><span class="built_in">console</span>.log(agent.name);  <span class="comment">// Tom</span></span><br><span class="line"><span class="built_in">console</span>.log(agent.age);   <span class="comment">// 23</span></span><br><span class="line">agent.money = <span class="number">90000</span>;</span><br><span class="line"><span class="built_in">console</span>.log(agent.money);  <span class="comment">// undefined</span></span><br><span class="line">agent.money = <span class="number">100000</span>;</span><br><span class="line"><span class="built_in">console</span>.log(agent.money);  <span class="comment">// 100000</span></span><br></pre></td></tr></table></figure></li>
<li><p><code>Reflect</code></p>
<p>  Reflect 提供了一些操作对象的 API。其中 Proxy 中 get 和 set 方法，默认使用的是 <code>Reflect</code> 提供的<code>Reflect.get</code> 和 <code>Reflect.set</code>，分别进行数据中对应属性值的获取和设置。相较于传统的设置对象方法 <code>Object.defineProperty(obj, key, value)</code>，<code>Reflect</code> 有以下几个优点。</p>
<ol>
<li>将挂载在 <code>Object</code> 上的方法，看起来像是 js 底层内部写法，放到 Reflect 对象上</li>
<li><code>Object.defineProperty(obj, key, value)</code> 如果有错会抛出异常，需要 <code>try...catch</code> 捕获。使用 <code>Reflect</code> 有错误，会返回 false，可以省略 <code>try...catch</code>。</li>
</ol>
</li>
<li><p><code>class</code></p>
</li>
</ol>
</li>
<li><p>新增的方法</p>
<ol>
<li><p><code>Promise</code> </p>
</li>
<li><p><code>async/await</code></p>
</li>
<li><p><code>generator</code> 生成器</p>
<ol>
<li><p>语法</p>
<p> 使用 <code>function*</code> 定义一个 generator 生成器。示例：</p>
 <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span>* <span class="title">G1</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">const</span> a1 = <span class="keyword">yield</span> <span class="number">1</span>;</span><br><span class="line">  <span class="keyword">const</span> b1 = <span class="keyword">yield</span> <span class="number">2</span>;</span><br><span class="line">  <span class="keyword">return</span> <span class="number">3</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> g1 = G1();</span><br><span class="line">g1.next();</span><br><span class="line">g1.next();</span><br><span class="line">g1.next();</span><br></pre></td></tr></table></figure></li>
<li><p>特点</p>
<ol>
<li>执行 <code>G1()</code> 返回一个生成器对象 <code>g1</code>，它也可称为 <code>iterator</code> 对象</li>
<li>通过 <code>g1.next()</code> 可向下执行语句，直到遇到 <code>yield</code> 暂停执行</li>
<li>执行<code>.next()</code> 方法获取到的返回值为 <code>&#123;value: xx, done: false&#125;</code>；其中 <code>value</code> 的值即是当前遇到的 <code>yield</code> 后的表达式的值， <code>done</code> 表示当前是否执行结束</li>
<li>暂停后，可以通过 <code>g1.next()</code> 再次向下执行，此时若给 <code>next()</code> 函数传参，则参数会赋值给上一次暂停的 <code>yield</code> 语句 <code>=</code> 左侧的变量。</li>
</ol>
</li>
<li><p>应用</p>
<ol>
<li>优化异步任务的写法</li>
<li>生成可使用 <code>for...of</code> 遍历的对象</li>
</ol>
</li>
</ol>
</li>
<li><p><code>for...of</code></p>
<ol>
<li><p>实现原理</p>
<p> 对象若实现了 <code>iteratable</code> 接口，则可以使用 <code>for...of</code> 进行遍历。</p>
</li>
<li><p><code>iteratable</code> 接口</p>
<p> 接口要求当前对象有 <code>Symbol.iterator</code> 属性，且该属性对应的值是个函数，执行函数后返回一个 <code>iterator</code> 对象，其具有 <code>next</code> 属性。<code>next</code> 的值是个函数，函数返回的值 <code>&#123;value: xx, done: false&#125;</code>。用代码演示如下：</p>
 <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> obj = &#123; <span class="attr">name</span>: <span class="string">&#x27;xiao&#x27;</span>, <span class="attr">age</span>: <span class="number">23</span> &#125;;</span><br><span class="line"></span><br><span class="line">obj[<span class="built_in">Symbol</span>.iterator] = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">const</span> keys = <span class="built_in">Object</span>.keys(obj);</span><br><span class="line">  <span class="keyword">let</span> index = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> &#123;</span><br><span class="line">    next: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">      <span class="keyword">return</span> &#123;</span><br><span class="line">        value: obj[keys[index]],</span><br><span class="line">        done: index++ &gt;= keys.length</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">let</span> value <span class="keyword">of</span> obj) &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(value);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>通过 <code>generator 生成器</code> 将普通对象转化为具有 <code>iterator</code> 对象</p>
<ol>
<li><p><code>generator</code> 生成的是一个 <code>iterator</code> 对象，所以可以使用 for…of 遍历</p>
</li>
<li><p>举例</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> info = &#123; <span class="attr">name</span>: <span class="string">&#x27;xiao&#x27;</span>, <span class="attr">age</span>: <span class="number">23</span> &#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span>* <span class="title">G2</span>(<span class="params">obj</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">const</span> keys = <span class="built_in">Object</span>.keys(obj);</span><br><span class="line"></span><br><span class="line">  <span class="keyword">for</span>(<span class="keyword">let</span> i = <span class="number">0</span>, len = keys.length; i &lt; len; i++) &#123;</span><br><span class="line">    <span class="keyword">yield</span> obj[keys[i]];</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">let</span> item <span class="keyword">of</span> G2(info)) &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(item);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ol>
</li>
</ol>
</li>
</ol>
</li>
</ol>
 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <div class="declare">
      <ul class="post-copyright">
        <li>
          <i class="ri-copyright-line"></i>
          <strong>版权声明： </strong>
          
          本博客所有文章除特别声明外，著作权归作者所有。转载请注明出处！
          
        </li>
      </ul>
    </div>
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=https://xiaofujun.gitee.io/2021/04/27/%E5%86%8D%E8%AF%86-ES6/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2021/04/27/%E5%88%9D%E5%A7%8B-typescript/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            初始 typescript
          
        </div>
      </a>
    
    
      <a href="/2021/04/22/%E5%BC%82%E6%AD%A5%E5%87%BD%E6%95%B0/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">异步函数</div>
      </a>
    
  </nav>

  
   
     
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2015-2022
        <i class="ri-heart-fill heart_icon"></i> 小付君
      </li>
    </ul>
    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
      </li>
    </ul>
  </div>
</footer>    
    </main>
    <div class="float_btns">
      <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

    </div>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="君书"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/img/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/img/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-3.6.0.min.js"></script>
 
<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->
 
<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: ".tocbot",
    contentSelector: ".article-entry",
    headingSelector: "h1, h2, h3, h4, h5, h6",
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: "main",
    positionFixedSelector: ".tocbot",
    positionFixedClass: "is-position-fixed",
    fixedSidebarOffset: "auto",
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link
  rel="stylesheet"
  href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css"
/>
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->
 <!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script> 
<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->

<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->
 
<link rel="stylesheet" href="/css/clipboard.css">
 <script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>
 
<!-- CanvasBackground -->

<script>
  if (window.mermaid) {
    mermaid.initialize({ theme: "forest" });
  }
</script>


    
    

  </div>
</body>

</html>